package org.dromara.canteen.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.dromara.canteen.domain.CanteenCertificates;
import org.dromara.canteen.domain.vo.CanteenCertificatesVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.school.domain.Campus;
import org.dromara.system.domain.SysOss;
import org.springframework.stereotype.Service;
import org.dromara.canteen.domain.bo.CanteenCookingBo;
import org.dromara.canteen.domain.vo.CanteenCookingVo;
import org.dromara.canteen.domain.CanteenCooking;
import org.dromara.canteen.mapper.CanteenCookingMapper;
import org.dromara.canteen.service.ICanteenCookingService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 烹饪管理Service业务层处理
 *
 * @author Gjb
 * @date 2025-07-31
 */
@RequiredArgsConstructor
@Service
public class CanteenCookingServiceImpl  extends MPJBaseServiceImpl<CanteenCookingMapper,CanteenCooking> implements ICanteenCookingService {

    private final CanteenCookingMapper baseMapper;

    /**
     * 查询烹饪管理
     */
    @Override
    public CanteenCookingVo queryById(Long cookingId){
        return baseMapper.selectVoById(cookingId);
    }

    /**
     * 查询烹饪管理列表
     */
    @Override
    public TableDataInfo<CanteenCookingVo> queryPageList(CanteenCookingBo bo, PageQuery pageQuery) {
        TenantHelper.clearDynamic();
        MPJLambdaWrapper<CanteenCooking> mpj= JoinWrappers.lambda(CanteenCooking.class)
            .selectAll(CanteenCooking.class)
            .selectAs(Campus::getCampusName, CanteenCookingVo::getSchoolCampusName)
            .leftJoin(Campus.class,Campus::getCampusId,CanteenCooking::getSchoolCampusId)
            .selectAs(SysOss::getUrl,CanteenCookingVo::getOssUrl)
            .leftJoin(SysOss.class,SysOss::getOssId,CanteenCooking::getCookingPhoto)
            .logicDelToOn();
        if(bo.getSchoolCampusId()!=null){
            mpj.eq(CanteenCooking::getSchoolCampusId,bo.getSchoolCampusId());
        }
//        LoginUser loginUser = LoginHelper.getLoginUser();
//        UserType userType = UserType.getUserType(loginUser.getUserType());
//        if(bo.getTenantId()!=null){
//            mpj.eq(Campus::getTenantId,bo.getTenantId());
//        }
//        switch (userType) {
//            case SYS_USER:
//                break;
//            case SCHOOLADMIN:
//                break;
//            case TEACHER:
//                break;
//            default:
//                return null;
//        }
        IPage<CanteenCookingVo> ipages=baseMapper.selectJoinPage(
            new Page<CanteenCookingVo>(pageQuery.getPageNum(), pageQuery.getPageSize()),
            CanteenCookingVo.class,mpj
        );
        TableDataInfo<CanteenCookingVo> result = TableDataInfo.build(ipages);
        return result;










//        LambdaQueryWrapper<CanteenCooking> lqw = buildQueryWrapper(bo);
//        Page<CanteenCookingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
    }

    /**
     * 查询烹饪管理列表
     */
    @Override
    public List<CanteenCookingVo> queryList(CanteenCookingBo bo) {
        LambdaQueryWrapper<CanteenCooking> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CanteenCooking> buildQueryWrapper(CanteenCookingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CanteenCooking> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCookingName()), CanteenCooking::getCookingName, bo.getCookingName());
        lqw.eq(StringUtils.isNotBlank(bo.getCookingIngredients()), CanteenCooking::getCookingIngredients, bo.getCookingIngredients());
        lqw.eq(StringUtils.isNotBlank(bo.getCookingPhoto()), CanteenCooking::getCookingPhoto, bo.getCookingPhoto());
        return lqw;
    }

    /**
     * 新增烹饪管理
     */
    @Override
    public Boolean insertByBo(CanteenCookingBo bo) {
        CanteenCooking add = MapstructUtils.convert(bo, CanteenCooking.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCookingId(add.getCookingId());
        }
        return flag;
    }

    /**
     * 修改烹饪管理
     */
    @Override
    public Boolean updateByBo(CanteenCookingBo bo) {
        CanteenCooking update = MapstructUtils.convert(bo, CanteenCooking.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CanteenCooking entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除烹饪管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
